package com.sugon.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.RandomUtil;
import com.sugon.dao.ToolsCheckPlanItemDao;
import com.sugon.entity.ToolsCheckPlanItemEntity;
import com.sugon.entity.ToolsCheckPlanItemToolsEntity;
import com.sugon.service.ToolsCheckPlanItemService;
import com.sugon.service.ToolsCheckPlanItemToolsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 工装周期性检测计划明细表，设备关联Service实现类
 *
 * @author sugon
 * @date 2021-09-23 16:26:40
 */
@Service("toolsCheckPlanItemService")
public class ToolsCheckPlanItemServiceImpl implements ToolsCheckPlanItemService {

    @Autowired
    private ToolsCheckPlanItemDao toolsCheckPlanItemDao;
    @Autowired
    private ToolsCheckPlanItemToolsService toolsCheckPlanItemToolsService;


    @Override
    public void copy(String originPlanId, String targetPlanId) {
        Map<String, Object> queryParam = MapUtil.newHashMap();
        queryParam.put("planId", originPlanId);
        List<ToolsCheckPlanItemEntity> planItemList = queryList(queryParam);
        if (CollectionUtil.isEmpty(planItemList)) {
            return;
        }
        ToolsCheckPlanItemEntity targetItem;
        ToolsCheckPlanItemToolsEntity targetItemTools;
        for (ToolsCheckPlanItemEntity planItem : planItemList) {
            targetItem = new ToolsCheckPlanItemEntity();
            BeanUtil.copyProperties(planItem, targetItem, "id", "planId");
            targetItem.setPlanId(targetPlanId);
            int saveFlag = save(targetItem);
            if (saveFlag > 0) {
                // 复制工装检测计划明细-工装列表
                List<ToolsCheckPlanItemToolsEntity> planItemToolsList = toolsCheckPlanItemToolsService.queryByItemId(planItem.getId());
                if (CollectionUtil.isEmpty(planItemToolsList)) {
                    continue;
                }
                for (ToolsCheckPlanItemToolsEntity itemTools : planItemToolsList) {
                    targetItemTools = new ToolsCheckPlanItemToolsEntity();
                    BeanUtil.copyProperties(itemTools, targetItemTools, "id", "itemId");
                    targetItemTools.setItemId(targetItem.getId());
                    toolsCheckPlanItemToolsService.save(targetItemTools);
                }
            }
        }
    }

    @Override
    public ToolsCheckPlanItemEntity queryObject(String devId) {
        return toolsCheckPlanItemDao.queryObject(devId);
    }

    @Override
    public List<ToolsCheckPlanItemEntity> queryList(Map<String, Object> map) {
        return toolsCheckPlanItemDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return toolsCheckPlanItemDao.queryTotal(map);
    }

    @Override
    public int save(ToolsCheckPlanItemEntity toolsCheckPlanItem) {
        toolsCheckPlanItem.setId(RandomUtil.randomString(32));
        return toolsCheckPlanItemDao.save(toolsCheckPlanItem);
    }

    @Override
    public int update(ToolsCheckPlanItemEntity toolsCheckPlanItem) {
        return toolsCheckPlanItemDao.update(toolsCheckPlanItem);
    }

    @Override
    public int delete(String devId) {
        return toolsCheckPlanItemDao.delete(devId);
    }

    @Override
    public int deleteBatch(String[] devIds) {
        return toolsCheckPlanItemDao.deleteBatch(devIds);
    }
}
